package fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Contants.Url;
import adapter.JingXuanAdapter;
import bean.JingXuanInfo;
import utils.OkHttpClientUtils;
import yztc.com.shiyuanbaoyou.R;

/**
 * Created by My on 2016/9/23.
 */
public class JingXuanFragment extends Fragment {
    private int page = 1;
    private JingXuanAdapter adapter;
    private com.handmark.pulltorefresh.library.PullToRefreshGridView gridView;
    private List<JingXuanInfo.RowsBean> list = new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    list = (List<JingXuanInfo.RowsBean>) msg.obj;
                    refresh();
                    break;
                case 2:
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jingxuan, null);
        gridView = (com.handmark.pulltorefresh.library.PullToRefreshGridView) view.findViewById(R.id.gv_jingxuan);
        httpget();
        return view;
    }

    private void refresh() {
        adapter = new JingXuanAdapter(list, getContext());
        gridView.setAdapter(adapter);

        gridView.setMode(PullToRefreshBase.Mode.BOTH);
        gridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                httpget();
                gridView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                page++;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String jsonString = OkHttpClientUtils.getStringFromURL(getContext(), Url.URL_JINGXUAN1 + page + Url.URL_JINGXUAN2, "");
                            JingXuanInfo data = null;
                            data = JSON.parseObject(jsonString, JingXuanInfo.class);
                            List<JingXuanInfo.RowsBean> allList = data.getRows();
                            list.addAll(allList);
                            Message msg = Message.obtain();
                            msg.what = 2;
                            msg.obj = allList;
                            handler.sendMessage(msg);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                gridView.onRefreshComplete();
            }
        });
    }

    private void httpget() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString = OkHttpClientUtils.getStringFromURL(getContext(), Url.URL_JINGXUAN1 + 1 + Url.URL_JINGXUAN2, "");
                    JingXuanInfo data = null;
                    data = JSON.parseObject(jsonString, JingXuanInfo.class);
                    list = data.getRows();
                    Message msg = Message.obtain();
                    msg.obj = list;
                    msg.what = 1;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}
